<?php
namespace app\modules\web\controllers;

use \app\modules\web\controllers\common\BaseController;
use \app\common\services\UploadService;
/**
* 
*/
class UploadController extends BaseController
{
	private $allow_file_type=['jpg','gif','png'];
	/**
	 * 上传接口
	 * bucket:avatar/brand/book
	 */
	public function actionPic(){
		$bucket=$this->post("bucket","");

		$callback="window.parent.upload";
		if(!$_FILES || !$_FILES['pic']){
			return "<script>{$callback}.error('请选择文件之后再提交!')</script>";
		}
		$file_name=$_FILES['pic']['name'];
		$tmp_file_extend=explode(".", $file_name);
		
		if(!in_array(strtolower(end($tmp_file_extend)),$this->allow_file_type)){
			return "<script>{$callback}.error('请上传指定文件后缀的文件')</script>";
		}
		//上传逻辑
		$ret=UploadService::uploadByFile($file_name,$_FILES['pic']['tmp_name'],$bucket);
		if(!$ret){
			return "<script>{$callback}.error('".UploadService::getErrorMsg()."')</script>";
		}
		return "<script>{$callback}.success('".$ret['path']."')</script>";
	}
}
?>